import notification from "@/services/notification";
import { observable, action, makeObservable } from 'mobx';
import Payment from "@/services/payment";
import PaymentActivity from "@/services/paymentactivity";
import PaymentModel from "@/store/models/PaymentModel";
import GlobalFunctions from '@/store/globalFunctions';
import Workflow from "@/services/workflow";
import CheckDialog from "@/components/workflow/CheckDialog";
import { currentUser } from "@/services/auth";
import Enums from "@/services/enums";
import moment from "moment";
import ProjectDetail from "@/services/projectdetail";

class PaymentStore {
  @observable paymentModel = new PaymentModel(0,0);
  constructor() {
    makeObservable(this)
  }
  @action initStore(id, projectdetailid, paytype) {
    this.init(id, projectdetailid, paytype);
  }
  init(id, projectdetailid, paytype) {
    if (id !== "0" && id !== 0) {
      Payment.get({ id: id }).then(data => {
        this.paymentModel = PaymentModel.fromJS(data,paytype);
        Workflow.edit({ bill: "payment", billtype: 0, billid: id, userid: currentUser.id }).then(data1 => {
          if (data1.length > 0) {
            this.paymentModel.setBilledit(data1[0]);
          }
        }).catch(error => {
        });
        PaymentActivity.getlist({ paymentid: id, page: 1, pagesize: 15 }).then(data => {
          this.paymentModel.setPaymentactivitydata(data.data, paytype, 0);
        });
      }).catch(error => {
        this.paymentModel = new PaymentModel(0,paytype);
        this.getcode("payment", moment(), 0, "code");
      });
    } else {
      Payment.getid({ id: 0 }).then(data => {
        this.paymentModel = new PaymentModel(data[0].id,paytype);
        this.getcode("payment", moment(), 0, "code");
        this.getemployeedata(id, paytype, projectdetailid);
        if (projectdetailid === "0" || projectdetailid === 0) {
          PaymentActivity.getlist({ paymentid: id, page: 1, pagesize: 15 }).then(data => {
            this.paymentModel.setPaymentactivitydata(data.data, paytype, projectdetailid);
          });
        } else {
          PaymentActivity.getunlist({ paymentid: id, page: 1, pagesize: 15, projectdetailid: projectdetailid, paytype: paytype }).then(data => {
            this.paymentModel.setPaymentactivitydata(data.data, paytype, projectdetailid);
          });
        }
      });
    }
  }
  getcode(tblname, bill_date, type, tblfield) {
    Enums.getcode({ tblname: tblname, bill_date: bill_date, type: type, tblfield: tblfield, }).then(data => {
      this.paymentModel.setCode(data[0].code);
    }).catch(error => {
      notification.error(error.message, error.detailedMessage);
    });
  };
  getemployeedata(id, paytype, projectdetailid) {
    ProjectDetail.getsellist({ bytstatus: '已签', billid: id, paytype: paytype }).then(data => {
      this.paymentModel.setEmployeedata(data, paytype, projectdetailid);
    });
  }
  dataSave() {
    if (GlobalFunctions.isEmptyobj(this.paymentModel.code)) {
      notification.error("编号不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.paymentModel.name)) {
      notification.error("名称不能为空");
      return;
    }
    if (this.paymentModel.paytype === 0) {
      if (GlobalFunctions.isEmptyobj(this.paymentModel.employeeid)) {
        notification.error("请选择人员");
        return;
      } else if (this.paymentModel.employeeid === 0) {
        notification.error("请选择人员");
        return;
      }
    } else {
      if (GlobalFunctions.isEmptyobj(this.paymentModel.customerid)) {
        notification.error("请选择客户");
        return;
      } else if (this.paymentModel.customerid === 0) {
        notification.error("请选择客户");
        return;
      }
    }
    if (GlobalFunctions.isEmptyobj(this.paymentModel.paydate)) {
      if (this.paymentModel.paytype === 0) {
        notification.error("付款日期不能为空");
      } else {
        notification.error("收款日期不能为空");
      }
      return;
    }

    if (GlobalFunctions.isEmptyobj(this.paymentModel.signingtime)) {
      notification.error("签约时间不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.paymentModel.expirationtime)) {
      notification.error("到期时间不能为空");
      return;
    }
    if (this.paymentModel.paymentactivitydata.length < 1) {
      notification.error("款项明细不能为空");
      return;
    }

    let amounttemp = 0;
    let otheramounttemp = 0;
    for (var item in this.paymentModel.paymentactivitydata) {
      amounttemp = amounttemp + Number(this.paymentModel.paymentactivitydata[item].amount);
      otheramounttemp = otheramounttemp + Number(this.paymentModel.paymentactivitydata[item].otheramount);
    }
    if (amounttemp !== Number(this.paymentModel.amount)) {
      notification.error("金额: " + this.paymentModel.amount + " 与款项明细金额合计：" + amounttemp + " 不相等");
      return;
    }
    if (otheramounttemp !== Number(this.paymentModel.otheramount)) {
      notification.error("其他金额: " + this.paymentModel.otheramount + " 与款项明细其他金额合计：" + otheramounttemp + " 不相等");
      return;
    }
    let data = {
      id: this.paymentModel.id,
      bill_date: this.paymentModel.bill_date,
      customerid: this.paymentModel.customerid,
      employeeid: this.paymentModel.employeeid,
      type: this.paymentModel.paytype,
      code: this.paymentModel.code,
      name: this.paymentModel.name,
      amount: this.paymentModel.amount,
      othertype: this.paymentModel.othertype,
      otheramount: this.paymentModel.otheramount,
      payname: this.paymentModel.payname,
      accountname: this.paymentModel.accountname,
      accountcode: this.paymentModel.accountcode,
      accountbank: this.paymentModel.accountbank,
      paydate: this.paymentModel.paydate,
      message: this.paymentModel.message,
      signingtime: this.paymentModel.signingtime,
      expirationtime: this.paymentModel.expirationtime,
      status: this.paymentModel.status,
      orgunitid: this.paymentModel.orgunitid,
      userid: this.paymentModel.userid,
      paymentactivitydata: this.paymentModel.paymentactivitydata,
    }
    if (this.paymentModel.isnew) {
      Payment.create(data)
        .then(() => {
          this.init(this.paymentModel.id,0,this.paymentModel.paytype);
          notification.success("新增成功.");
        })
        .catch(error => {
          notification.error("保存失败", error.response.data.message);
        });
    } else {
      Payment.save(data)
        .then(() => {
          notification.success("修改成功.");
        })
        .catch(error => {
          notification.error("保存失败", error.response.data.message);
        });
    }
  };
  dataDelete() {
    Payment.delete({ id: this.paymentModel.id }).then(() => {
      notification.success("删除成功.");
      this.init(0,0,this.paymentModel.paytype);
    }).catch(error => {
      notification.error("删除失败", error.response.data.message);
    });
  };
  billCheck(userid, projectdetailid, paytype) {
    let item = {
      bill: "payment",
      billtype: 0,
      billid: this.paymentModel.id,
      next_type: 1,
      message: '<div>同意</div>',
      userid: userid,
      billdate: this.paymentModel.bill_date.format('YYYY-MM-DD'),
      orgunitid: this.paymentModel.orgunitid,
      status: this.paymentModel.status,
    }
    if (this.paymentModel.status === 0) {

      Workflow.check(item).then(data => {
        if (data[0].flag === 0) {
          this.initStore(this.paymentModel.id, projectdetailid, paytype);

          notification.success(data[0].message);
        } else {
          notification.error(data[0].message);
        }
      })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    } else {
      CheckDialog.showModal({ bill_check: item })
        .onClose((result = {}) => {
          if (result.success) {
            this.initStore(this.paymentModel.id, projectdetailid, paytype);
          }
        }).onDismiss(() => {
        });
    }
  };
}

export default PaymentStore;